﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using EzyGoEzySystem.Models;
using System.Text;
using System.Net;
using System.IO;
using EzyGoEzySystem.Services;

namespace EzyGoEzySystem.Controllers
{
    public class PaymentController : Controller
    {

        //
        // GET: /Account/LogOn
        EzyGoEzyDBContext dbContext = new EzyGoEzyDBContext();

        public ActionResult Pay()
        {


            //return Content(String.Format("Hello, {0}!", Session["AccountID"]));
            PaymentModel model = new PaymentModel();
            model.Amounts = new SelectList(GetAmounts(), "Amount", "Display");
            return View(model);


        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult Pay(PaymentModel model, string returnUrl)
        {
            dbContext = new EzyGoEzyDBContext();
            //PaymentJournalEntry je = new PaymentJournalEntry();
            //Account account = new Account();
            var account = (Account)Session["Account"];
            
            //je.Account = account;
            var je = new PaymentJournalEntry
            {
                Invoice = Serializer.RandomString(),
                Amount = 0,
                PaymentStatus = "Pending",
                AccountID = account.ID
                //Account = account
            };

            Session["PaymentInvoice"] = je.Invoice;
            dbContext.PaymentJournalEntries.Add(je);

            try
            {
                if (int.Parse(model.Amount) == 10)
                {
                    je.Amount = 10;
                    dbContext.SaveChanges();
                    //dbContext.Dispose();
                    return RedirectToAction("Pay10");
                }
                else if (int.Parse(model.Amount) == 20)
                {
                    je.Amount = 20;
                    dbContext.SaveChanges();
                    //dbContext.Dispose();
                    return RedirectToAction("Pay20");
                }
                else if (int.Parse(model.Amount) == 50)
                {
                    je.Amount = 50;
                    dbContext.SaveChanges();
                    //dbContext.Dispose();
                    return RedirectToAction("Pay50");
                }
                else
                {
                    //dbContext.SaveChanges();
                    //dbContext.Dispose();
                    return Content(String.Format("Invalid Amount, {0}!", model.Amount));
                }
            }
            finally
            {
                dbContext.Dispose();
            }
        }

        public ActionResult Pay10()
        {
            return View();
        }
        public ActionResult Pay20()
        {
            return View();
        }
        public ActionResult Pay50()
        {
            return View();
        }

        private List<PaymentAmount> GetAmounts()
        {
            List<PaymentAmount> amounts = new List<PaymentAmount>();
            amounts.Add(new PaymentAmount(10, "$S 10"));
            amounts.Add(new PaymentAmount(20, "$S 20"));
            amounts.Add(new PaymentAmount(50, "$S 50"));
            return amounts;
        }

        public class PaymentAmount
        {
            public double Amount { get; set; }
            public string Display { get; set; }
            public PaymentAmount(double amount, string display)
            {
                this.Amount = amount;
                this.Display = display;
            }
        }
    }
}
